package xyz.migle.sc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
//import org.springframework.context.annotation.Configuration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//TODO: spring-boot-admin接收client的信息
//@Configuration
//@EnableAutoConfiguration
////@EnableAdminServer //激活spring-boot-admin,集群中有一个就够了
//@EnableDiscoveryClient
//@RestController


//@EnableScheduling
@SpringCloudApplication
public class ScApplication {

    private static final Logger logger = LoggerFactory.getLogger(ScApplication.class);

//    @RequestMapping("/test")
//    public String home() {
//        logger.info("Hello World,here is  a sc-demo");
//        logger.trace("trace 成功了");
//        logger.debug("debug 成功了");
//        logger.info("info 成功了");
//        logger.warn("warn 成功了");
//        logger.error("error 成功了");
//        return "Hello World,here is  a sc-demo";
//    }


    /**
     * 自定义定时定时任务线程池
     * 因为所有@Scheduled 任务默认会单线程执行，所以会出现调度不及时的情况
     * @return
     */
    @Value("${task.threadPool.size:2}")
    private int taskThreadPoolSize;

    @Bean(name = "TaskPool")
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(taskThreadPoolSize);
        return taskScheduler;
    }

//    @Bean(destroyMethod="shutdown")
//    public Executor taskExecutor() {
//        return  new ScheduledThreadPoolExecutor(20, new ThreadFactory() {
//            private AtomicInteger max = new AtomicInteger(0);
//            @Override
//            public Thread newThread(Runnable r) {
//                return new Thread(r, "mySchedulAnno-" + max.incrementAndGet());
//            }
//        });
//    }


    public static void main(String[] args) {
        SpringApplication.run(ScApplication.class, args);

    }
}


